#!/bin/bash
beesu_requires "gnome-terminal" "gnome-terminal"
base="$(echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g')"
if [ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then
     dir="$base"
else 
     while [ ! -z "$1" -a ! -d "$base/$1" ]; do shift; done
     dir="$base/$1"
fi
# beesu - gnome-terminal --working-directory="$dir"
# We now have to place beesu at the end because gnome-terminal throws dbus errors and fails otherwise.
#
# Also use different commands based upon readability of directory. An unreadable directory results in 
# failure of gnome-terminal --working-directory="$dir" -e 'beesu /bin/bash' and gnome-terminal -e 'beesu
# cd "'$dir'" && /bin/bash' fails because a directory name containing spaces has its name truncated
# due to a bug in an unknown location. Since most unreadable directory names do not contain spaces,
# and it is possible that a readable directory name will contain spaces, this seems a workable
# workaround solution to the problems. 
filepath=$(echo -e "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | 
         awk 'BEGIN { FS = "\n" } { printf "\"%s\"\n", $1 }' | sed -e s#\"\"##)
attribs=$(echo "$filepath" | xargs ls -l | awk '{print $1}')
folder_readable=$(echo "$attribs" | cut -c2-2 | sed 's/r/true/g' | sed 's/-/false/g')
if [ ! $folder_readable ]; then
    gnome-terminal -e 'beesu cd "'$dir'" && /bin/bash'
else
    gnome-terminal --working-directory="$dir" -e 'beesu /bin/bash'
fi
# F12
/bin/true
